package com.n9.buymsg.view.table;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * table辅助类
 * @author ice
 */
public class JTableHelper {
    private Map<Integer, JTableColumnField> tableColumnFields = new ConcurrentHashMap<>();

    public JTableHelper(Class<?> clazz) {
        initJTableHelper(clazz);
    }

    /**
     * 初始化JTableHelper
     * @param clazz
     */
    private void initJTableHelper(Class<?> clazz) {
        JTable jTable = clazz.getAnnotation(JTable.class);
        if (Objects.isNull(jTable)) {
            throw new JTableException("JTable注解不存在");
        }
        JTableColumn[] columns = jTable.columns();
        for (int i = 0; i < columns.length; i++) {
            JTableColumn c = columns[i];

            JTableColumnField jtcf = new JTableColumnField();
            jtcf.setTitle(c.title());
            jtcf.setPreferredWidth(c.preferredWidth());
            jtcf.setMinWidth(c.minWidth());
            jtcf.setMaxWidth(c.maxWidth());
            jtcf.setRowIndex(c.rowIndex());
            if (!c.rowIndex()) {
                Field field = null;
                try {
                    field = clazz.getDeclaredField(c.fieldName());
                    field.setAccessible(true);
                    jtcf.setField(field);
                } catch (NoSuchFieldException e) {
                    throw new JTableException(String.format("field :%s not found %s", c.fieldName(), clazz.getName()));
                }
            }
            tableColumnFields.put(i,jtcf);
        }
    }

    /**
     * 获取列名
     * @param columnIndex
     * @return
     */
    public String getColumnName(int columnIndex) {
        return getColumnFieldByIndex(columnIndex).getTitle();
    }

    public int getColumnCount() {
        return tableColumnFields.size();
    }

    public int getPreferredWidth(int columnIndex) {
        return getColumnFieldByIndex(columnIndex).getPreferredWidth();
    }


    private JTableColumnField getColumnFieldByIndex(int columnIndex) {
        JTableColumnField jtcf = tableColumnFields.get(columnIndex);
        if (Objects.isNull(jtcf)) {
            throw new JTableException("列不存在:" + columnIndex);
        }
        return jtcf;
    }

    public int getMinWidth(int columnIndex) {
        return getColumnFieldByIndex(columnIndex).getMinWidth();
    }

    public int getMaxWidth(int columnIndex) {
        return getColumnFieldByIndex(columnIndex).getMaxWidth();
    }

    public Object getColumnValue(Object rowDataModel, int rowIndex, int columnIndex) {
        JTableColumnField jtcf = getColumnFieldByIndex(columnIndex);

        if (jtcf.isRowIndex()) {
            return String.format("%04d", rowIndex + 1);
        } else {
            Field field = jtcf.getField();
            try {
                return field.get(rowDataModel);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new JTableException(e.getMessage());
            }
        }
    }

    public void setColumnValue(Object rowDataModel, int columnIndex, Object value) {
        JTableColumnField jtcf = getColumnFieldByIndex(columnIndex);

        if (!jtcf.isRowIndex()) {
            Field field = jtcf.getField();
            try {
                field.set(rowDataModel, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new JTableException(e.getMessage());
            }
        }
    }
}
